import pygame

from .oop import Singleton
from .resourse_manager import ImageResourceManager, SoundResourceManager

class Option(ImageResourceManager):
    def __init__(self):
        super().__init__()

    def enter(self):
        pass

class SinglePlayerOption(Option):
    image_path = "./assets/menu/single_player.png"
    def __init__(self, display):
        super().__init__()
        self.display = display
    
    def enter(self):
        print("Single Player Mode")

class MultiplePlayerOption(Option):
    image_path = "./assets/menu/multiple_player.png"
    def __init__(self, display):
        super().__init__()
        self.display = display

    def enter(self):
        print("Multiple Player Mode")

class Pointer(ImageResourceManager, Singleton):
    image_path = "./assets/menu/pointer.png"
    def __init__(self, display):
        super().__init__()
        self.display = display

class Menu(ImageResourceManager, SoundResourceManager, Singleton):
    image_path = "./assets/menu/menu_bg.jpg"
    sound_path = "./assets/menu/select_sound.wav"
    def __init__(self, display):
        super().__init__()

        self.display = display

        self.surface = pygame.transform.scale(
            self.surface,
            self.display.get_size()
        )

        self.selected_option = 0
        self.options = [
            SinglePlayerOption(self.surface),
            MultiplePlayerOption(self.surface)
        ]

        self.pointer = Pointer(self.surface)

    def event_handle(self, event):
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                self.selected_option -= 1
                self.sound.play()
            if event.key == pygame.K_DOWN:
                self.selected_option += 1
                self.sound.play()

            self.selected_option = (self.selected_option+2)%2
            
            if event.key == pygame.K_RETURN:
                self.options[self.selected_option].enter()

    def render(self):
        # render background
        self.display.blit(self.surface, (0,0))
        
        gap = 16
        # render options
        y_offset = 0
        for index in range(len(self.options)):
            option_surface = self.options[index].surface
            x = (self.surface.get_size()[0] - option_surface.get_size()[0])//2
            y = (self.surface.get_size()[1] - option_surface.get_size()[1])//2 + y_offset
            y_offset += option_surface.get_size()[1] + gap

            # render pointer
            if (index == self.selected_option):
                self.display.blit(self.pointer.surface, (x+option_surface.get_size()[0]+gap,y))

            self.display.blit(option_surface, (x,y))
